//
//  PhotoBrowserController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 07/05/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

private let photoBrowserCellReuseIdentifier = "pictureCell"
class PhotoBrowserController: UIViewController {

    var currentIndex: Int?
    var pictureURLs: [NSURL]?
    init(index: Int, urls: [NSURL])
    {
        // Swift语法规定, 必须先初始化本类属性, 再初始化父类
        currentIndex = index
        pictureURLs = urls
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        
        // 1.添加子控件
        view.addSubview(imgBg)
        view.addSubview(collectionView)
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        
        // 2.布局子控件
        closeBtn.snp.makeConstraints { (make ) -> Void in
            make.left.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-20-SAFE_SCREEN_BOTTOM)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        saveBtn.snp.makeConstraints { (make) -> Void in
            
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-20-SAFE_SCREEN_BOTTOM)
            make.width.equalTo(100)
            make.height.equalTo(35)
            
        }
        collectionView.snp.makeConstraints { (make) -> Void in
            make.center.equalToSuperview()
            make.size.equalToSuperview()
        }
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(PhotoBrowserCell.self, forCellWithReuseIdentifier: photoBrowserCellReuseIdentifier)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }

   @objc func close()
    {
        dismiss(animated: true, completion: nil)
    }
    @objc func save()
    {
        
    }
    
    
    // MARK: - 懒加载
    private lazy var closeBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("关闭", for: UIControlState.normal)
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        btn.backgroundColor = UIColor.darkGray
        
        btn.addTarget(self, action: #selector(close), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    private lazy var saveBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("保存", for: UIControlState.normal)
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        btn.backgroundColor = UIColor.darkGray
        
        btn.addTarget(self, action:#selector(save), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: PhotoBrowserLayout())
    private lazy var imgBg: UIImageView = {
       let bg = UIImageView()
        bg.frame = UIScreen.main.bounds
        bg.image = UIImage(named: "live_play_finish_bg")
        return bg
    }()
    

}

extension PhotoBrowserController:UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureURLs?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoBrowserCellReuseIdentifier, for: indexPath) as! PhotoBrowserCell
        
        cell.imageURL = pictureURLs![indexPath.item]
        cell.photoBrowserCellDelegate = self
        return cell
    }
    
}

extension PhotoBrowserController:PhotoBrowserCellDelegate{
    func photoBrowserCellDidClose(cell: PhotoBrowserCell) {
        dismiss(animated: true, completion: nil)
    }
    
}

class PhotoBrowserLayout : UICollectionViewFlowLayout {
    
    override func prepare() {
        itemSize = UIScreen.main.bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.horizontal
        
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.isPagingEnabled = true
        collectionView?.bounces =  false
    }
}
